import tkinter as tk

class LeadObj:
    Width = 100
    Height = 150
    PinIsOutsidePackage = True

    def __init__(self, canvas, edge_pos, location, radius=0):
        self.canvas = canvas
        self.edge_pos = edge_pos.upper()
        self.location = location
        self.radius = radius
        self.w = LeadObj.Width if self.edge_pos in ['U', 'D'] else LeadObj.Height
        self.h = LeadObj.Height if self.edge_pos in ['U', 'D'] else LeadObj.Width

#    def show(self):
        x, y = self.location
        half_w = self.w / 2
        half_h = self.h / 2
        if LeadObj.PinIsOutsidePackage :
            if self.edge_pos == 'L':
                x1 = x - self.w
                y1 = y - half_h
                x2 = x
                y2 = y + half_h
            elif self.edge_pos == 'R':
                x1 = x
                y1 = y - half_h
                x2 = x + self.w
                y2 = y + half_h
            elif self.edge_pos == 'U':
                x1 = x - half_w
                y1 = y - self.h
                x2 = x + half_w
                y2 = y
            elif self.edge_pos == 'D':
                x1 = x - half_w
                y1 = y
                x2 = x + half_w
                y2 = y + self.h
        else : # out
            if self.edge_pos == 'R':
                x1 = x - self.w
                y1 = y - half_h
                x2 = x
                y2 = y + half_h
            elif self.edge_pos == 'L':
                x1 = x
                y1 = y - half_h
                x2 = x + self.w
                y2 = y + half_h
            elif self.edge_pos == 'D':
                x1 = x - half_w
                y1 = y - self.h
                x2 = x + half_w
                y2 = y
            elif self.edge_pos == 'U':
                x1 = x - half_w
                y1 = y
                x2 = x + half_w
                y2 = y + self.h

        # Draw the rectangle without filling it
        self.canvas.create_rectangle(x1, y1, x2, y2, outline='black')

        # Draw the circle at the location if radius is greater than 0
        if self.radius > 0:
            self.canvas.create_oval(
                x - self.radius, y - self.radius,
                x + self.radius, y + self.radius,
                fill='black', outline='black'
            )

if __name__ == "__main__":
    # Set static attributes for LeadObj
    LeadObj.Width = 40
    LeadObj.Height = 80

    # Create main window
    root = tk.Tk()
    root.title("LeadObj Example")

    # Create a canvas
    canvas = tk.Canvas(root, width=1000, height=1000)
    canvas.pack()

    # Define locations and edges for each LeadObj
    locations_edges = [
        ((100, 300), 'L'),  # Left
        ((700, 300), 'R'), # Right
        ((400, 100), 'U'), # Up
        ((400, 500), 'D')  # Down
    ]

    # Create LeadObj instances at specified locations and edges
    leads = []
    for loc, edge in locations_edges:
        lead = LeadObj(canvas, edge_pos=edge, location=loc, radius=2)
        leads.append(lead)

    # Show all LeadObj instances
    #for lead in leads:
        #lead.show()

    # Display edge positions on the canvas
    for lead in leads:
        x, y = lead.location
        edge_text = f"Edge: {lead.edge_pos}"
        canvas.create_text(x, y - lead.h / 2 - 20, text=edge_text, font=('Helvetica', 10))

    # Run main loop
    root.mainloop()



